package com.turing.data.scripts.contract.filter

import com.turing.data.engine.CombineLongText
import com.turing.data.engine.TextUtils
import com.turing.data.engine.utils.ScriptsUtils
import com.turing.data.vo.out.review.DependParamsVO
import com.turing.data.vo.out.review.TaskElementVO
import org.apache.commons.lang.SerializationUtils
import com.turing.common.util.FileUtils
import com.turing.common.util.StringUtils

/**
 * 场景：资产管理合同
 * 版本：Version 1
 * 要素名称：管理费
 */

class ManagementCosts_30199_10_filter {

    static List< TaskElementVO.DrawResultVO > doConvert(DependParamsVO dependParams){

        String REGEX = /.*管理费.*/
        String TAIL = "】"

        def textPathMap = dependParams.textPathMap
        def JSONTextMap = dependParams.JSONTextMap
        List<TaskElementVO.DrawResultVO> trueValueSet = new ArrayList<>()
        List<TaskElementVO.DrawResultVO > resultVOs = ScriptsUtils.tryInitResult(dependParams)
        if (resultVOs != null && resultVOs.size() > 0) {
            for (int i = 0; i < resultVOs.size(); i++) {

                resultVOs[i].text = "56%"
                resultVOs[i].businessValue = resultVOs[i].text

                if (StringUtils.isEmpty(resultVOs.get(i).childTaskSn)) {
//                        return  resultVOs
                    continue
                }
                def rawPath = textPathMap.get(resultVOs.get(i).childTaskSn)
                def rawText = FileUtils.loadJson(rawPath)
                String trueValue = CombineLongText.fetchNearbyText(resultVOs.get(i), rawText, 20)

                if (trueValue =~ REGEX) {
                    def expandOffset = 0
                    def resultVO = resultVOs[i]
                    def start = resultVO.start
                    def end = resultVO.end
                    while (expandOffset < 3 && rawText.length() > end + expandOffset) {
                        def expandStr = rawText.substring(start, end + expandOffset)
                        if (expandStr =~ TAIL) {
                            def endOffset = expandOffset - 1
                            def fileTextStr = ""
                            fileTextStr = JSONTextMap.get(resultVO.childTaskSn)
                            TaskElementVO.DrawResultVO objectNew = (TaskElementVO.DrawResultVO) SerializationUtils.clone(resultVO)
                            objectNew = TextUtils.correctPositionInfoByOffset(objectNew, 0, endOffset, fileTextStr, rawText)
                            objectNew.text = rawText.substring(start, end + endOffset)
                            objectNew.businessValue = rawText.substring(start, end + endOffset)

//                            if(objectNew.text.contains("%")){
//                                objectNew.text = objectNew.text.replace("%","")
//                                objectNew.businessValue = objectNew.text
//                                objectNew.type = 1
//                            }
                            trueValueSet.add(objectNew)
                            return trueValueSet
                        }
                        expandOffset++
                    }

//                    if(resultVOs.get(i).text.contains("%")){
//                        resultVOs.get(i).text = resultVOs.get(i).text.replace("%","")
//                        resultVOs.get(i).businessValue = resultVOs.get(i).text
//                        resultVOs.get(i).type = 1
//                    }
                    trueValueSet.add(resultVOs.get(i))
                    return trueValueSet
                }
            }
        }


        return  resultVOs
    }

}
